package com.gimi.cloud.jimi.manage.consumer.service.order.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.common.utils.reqhead.Verification;
import com.gimi.cloud.jimi.dto.order.OrderDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.OrderFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.order.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderFeignService orderFeignService;

	private static Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

	@Override
	public Response OrderPageList(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		OrderDTO dto = JsonUtil.jsonToBean(requestBody, OrderDTO.class);
		Verification verification = EntityVerification.checkEntity(dto);
		if (!verification.getCheckResult()) {
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR, verification.getMsg());
			return resp;
		} else {
			return orderFeignService.getOrderPageList(dto);
		}
	}

	@Override
	public Response deleteOrder(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		OrderDTO dto = JsonUtil.jsonToBean(requestBody, OrderDTO.class);
		Verification verification = EntityVerification.checkEntity(dto);
		if (!verification.getCheckResult()) {
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR, verification.getMsg());
			return resp;
		} else {
			return orderFeignService.deleteOrder(dto);
		}
	}

	@Override
	public Response orderStatus(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		// OrderDTO dto = JsonUtil.jsonToBean(requestBody,OrderDTO.class);
		List<OrderDTO> orderList = JSONObject.parseArray(JSONObject.parseObject(requestBody).getString("orderStatus"),OrderDTO.class);
		Response resp = new Response();
		for (OrderDTO dto : orderList) {
			Verification verification = EntityVerification.checkEntity(dto);
			if (!verification.getCheckResult()) {
				resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR, verification.getMsg());
				break;
			} else {
				resp = orderFeignService.updateorderStatus(dto);
				break;
			}
		}
		return resp;
	}

	@Override
	public Response getSingleOrderBase(@RequestBody String requestBody, HttpServletRequest request,
			HttpServletResponse response) throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		OrderDTO dto = JsonUtil.jsonToBean(requestBody, OrderDTO.class);
		Verification verification = EntityVerification.checkEntity(dto);
		if (!verification.getCheckResult()) {
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR, verification.getMsg());
			return resp;
		} else {
			return orderFeignService.getSingleOrderBase(dto);
		}
	}

	@Override
	public Response updateOrderBase(@RequestBody String requestBody, HttpServletRequest request,
			HttpServletResponse response) throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		OrderDTO dto = JsonUtil.jsonToBean(requestBody, OrderDTO.class);
		Verification verification = EntityVerification.checkEntity(dto);
		if (!verification.getCheckResult()) {
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR, verification.getMsg());
			return resp;
		} else {
			return orderFeignService.updateOrderBase(dto);
		}
	}

	@Override
	public Response showOrderDetail(@RequestBody String requestBody, HttpServletRequest request,
			HttpServletResponse response) throws MsgException {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
		OrderDTO dto = JsonUtil.jsonToBean(requestBody, OrderDTO.class);
		Verification verification = EntityVerification.checkEntity(dto);
		if (!verification.getCheckResult()) {
			Response resp = new Response();
			resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR, verification.getMsg());
			return resp;
		} else {
			return orderFeignService.showOrderDetail(dto);
		}
	}

}
